import 'dart:convert';

import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/guide/scale_launchpage.dart';
import 'package:find_friend/pages/guide/sysmodel.dart';
import 'package:find_friend/pages/login&register/login.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class GuideScreen extends StatefulWidget {
  @override
  _GuideScreenState createState() => _GuideScreenState();
}

class _GuideScreenState extends State<GuideScreen> with SingleTickerProviderStateMixin {

  List<String> list = ["1","2","3"];
  TabController _tabController;
  int index = 0;

  

  @override
  void initState() {
    super.initState();
    _initRequest();
     _tabController = TabController(length: 3, vsync: this, initialIndex: 0);
    _recordFisrtApp();
  }

  // 记录登陆app
  _recordFisrtApp() async {
    SharedPreferences share = await SharedPreferences.getInstance();
    share.setBool(KEY_is_first_app, true);
  }

  _initRequest() {
    Networktool.request(API.sys, method: NETWORKHTTPMethod.GET,success: (data) async {
      if(data != null) {
        SysModel model = data['data'] != null ? SysModel.fromJson(data['data']) : null;
        list[0] = model.launch[0].url;
        list[1] = model.launch[1].url;
        _saveSys(model);
        if(mounted) {
          setState(() {
          
          });
        }
      }
    });
  }

  _saveSys(SysModel model) {
    Map<String, dynamic> str = model.toJson();
    SharedPreferences.getInstance().then((value) => value.setString(KEY_SYS_Data, json.encode(str)));
  }

  Widget _initPage(String element, double bottom) {
    if(list.indexOf(element) == 2) {
      return LoginScreen();
    } else {
      if(!element.contains("http")){
        return Container(alignment: Alignment.center,child: Icon(Icons.image, color: Colors.blue,),width: 50,);
      } else {
        return Image.network(element, fit: BoxFit.cover);
      }
    }
  }

  @override
  Widget build(BuildContext context) {

    final bottom = MediaQuery.of(context).padding.bottom;

    return Scaffold(
      backgroundColor: Colors.white,
      resizeToAvoidBottomPadding: false,
      body: Stack(
        children: [
          PageView(
            onPageChanged: (index){
              setState(() {
                this.index = index;
              });
              _tabController.index = index;
            },
            children: list.map((e) => _initPage(e, bottom)).toList(),
          ),
          
          Positioned(
            bottom: bottom + 20,
            child: Container(
              width: MediaQuery.of(context).size.width,
              alignment: Alignment.center,
              child: Opacity(
                opacity: index == 2 ? 0 : 1,
                child: TabPageSelector(
                  indicatorSize: 8,
                  color: Colors.white54,
                  selectedColor: Colors.white,
                  controller: _tabController,
                )
              ),
            )
          ),

          Positioned.fill(child: ScaleLaunch())
        ],
      ),
    );
  }
}